package cn.com.cabinet.manager.controller;



import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.base.util.DateUtil;
import com.cabinet.base.util.PageUtils;
import com.cabinet.base.util.PropertiesFileUtil;
import com.cabinet.base.util.QRCodeUtil;
import com.cabinet.easy.util.Page;
import com.cabinet.web.entity.Cabinet;
import com.cabinet.web.entity.Lattice;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;

import cn.com.cabinet.service.CabinetServiceImpl;
import cn.com.cabinet.service.LatticeServiceImpl;
import cn.com.cabinet.util.AsyncClientHttpExchangeStreaming;
import cn.com.cabinet.util.HttpRequest;
import cn.com.easy.dto.MessageDTO;
import cn.com.easy.dto.PageRequestParamDTO;
import cn.com.easy.utils.FastJSONUtils;
import cn.com.easy.utils.ResponseOutputUtils;

@Controller
@RequestMapping("/manager/cabinet")
public class CabinetController {
	@Autowired
	private CabinetServiceImpl cabinetServiceImpl;
	@Autowired
	private LatticeServiceImpl latticeServiceImpl;
	
	@RequestMapping()
	public String redirectIndex(){
		return "/Cabinet/index";
		
	}
	
	@RequestMapping("/list")
	public void getCabinetList(HttpServletRequest req, HttpServletResponse res, PageRequestParamDTO paramDto, Integer key, Integer status){
		try {
		int thispage = (paramDto.getPageNumber()-1)*10;
		
		List<Cabinet> cabinetList = cabinetServiceImpl.getCabinetList(thispage, paramDto.getPageSize());
		int totalcount = cabinetServiceImpl.getCabinetListTotal();
		Paginator p = new Paginator(thispage, paramDto.getPageSize(), totalcount);
		PageList<Cabinet> pageObject = new PageList<>(cabinetList, p);
		Page<Cabinet> page = PageUtils.getPage(pageObject);
		
		ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", true, page));
	} catch (Exception ex) {
		//logger.error("会员管理列表错误：", ex);
		ResponseOutputUtils.renderText(res, FastJSONUtils.toJsonString(MessageDTO.newInstance("", false, "会员管理列表错误")));
	}
		
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String addCabinet(String cClass,Integer cLatticeNum,String cProvince,String cCity,String cArea,int cCode,String Lng,String Lat) throws Exception{
		//System.out.println(cClass);
		//System.out.println(cLatticeNum);
		//System.out.println(cProvince);
		
		Cabinet ca = new Cabinet();
		
		ca.setcClass(cClass);
		if(cClass == "2" || cClass.equals("2")){
			System.out.println("++++++++++2+++++++++++");
			ca.setcProvince(cProvince);
			ca.setcArea(cArea);
			ca.setcCity(cCity);
			ca.setcLat(Lat);
			ca.setcLng(Lng);
			ca.setcMark(0);//未出租
		}else{
			ca.setcMark(1);//未出租
		}
		ca.setcCode(cCode);
		String cid = cClass+DateUtil.DateNum();
		ca.setcId(cid);
		ca.setcLatticeNum(cLatticeNum);
		//生成二维码
		String path = "/opt/upload/cabinet/qrcode/img";//二维码存储根路径
		
		String qrUrl = PropertiesFileUtil.getValue(PropertiesFileUtil.defaultFileName, "qr.url")+"QR/"+cid;
		//String qrUrl = posturl+"QR/"+cid;
		String qrname = QRCodeUtil.encode(qrUrl,"",path,true);//生成二维码，并返回二维码名称
		ca.setQrCodePath("upload/cabinet/qrcode/img/"+qrname);
		
		cabinetServiceImpl.addCabinet(ca);
		String posturl = PropertiesFileUtil.getValue(PropertiesFileUtil.defaultFileName, "post.url");
		String param = "cid="+cid+"&num="+cLatticeNum;
		String sendurl = posturl+"manager/cabinet/creatlattice?"+param;
		//HttpRequest.sendPost(posturl+"manager/cabinet/creatlattice", param);
		//AsyncClientHttpExchangeStreaming.AsyncClient(posturl+"manager/cabinet/creatlattice?"+param);
		
		return sendurl;
		
	}
	
	@RequestMapping("/creatlattice")
	@ResponseBody
	public void creatlattice(String cid,int num){
		System.out.println(cid+"----"+num);
		for(int i=0; i<num; i++){
			Lattice la = new Lattice();
			la.setCabId(cid);
			la.setLaNum(1);
			la.setlState(1);
			latticeServiceImpl.addLattice(la);
		}
	}
		
	
}
